#!/usr/bin/env python
import sqlite3
import re

MENTION = re.compile(r'@[^ ]+')
HASHTAG = re.compile(r'#[^ ]+')
QT = re.compile(r'^(R|Q)T ', re.I)
LINK = re.compile(r'http(s)?://\S+(\s|$)', re.I)

class Sampler():
	def __init__(self, dbname):
		self.conn = sqlite3.connect(dbname)
		self.cur = self.conn.cursor()
		"""delete old logs"""
		self.cur.execute("delete from texts where datetime(time) < datetime('now', '-1 months')")
		"""print count"""
		self.cur.execute("select count(*) from texts")
		count = self.cur.fetchone()[0]
		print "we have %d texts"%(count)
	def parse_text(self, text):
		"""remove mention and hashtags"""
		text = re.sub(MENTION, "", text)
		text = re.sub(HASHTAG, "", text)
		text = re.sub(QT, "", text)
		text = re.sub(LINK, "", text)

		text = text.strip()

		""" sqlite3 use '' instead of \' """
		self.cur.execute("insert into texts(text) values('%s')"%(text.replace("'", "''")))
		self.conn.commit()
